package cn.dida.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang3.StringUtils;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import cn.dida.exception.BizzException;
import cn.dida.model.Company;
import cn.dida.model.HttpResponseBodyWrapper;
import cn.dida.model.Pageable;
import cn.dida.model.UserSession;
import cn.dida.service.CompanyService;
/**
 *  <p>公司管理</p>
 * @author weiwei
 *
 */
@Controller
public class CompanyController extends BaseController {
//    @Autowired
    private CompanyService companyService;
    /**
     * 公司列表
     * @param request
     * @param keyword 查询条件
     * @param state 状态（1：正常、2：停用）
     * @param page 分页游标
     * @return ModelAndView 列表页面
     * @throws Exception
     */
    @GetMapping("/company")
    public ModelAndView company(HttpServletRequest request, String keyword, Integer state, Integer page) throws Exception {
        Map<String, Object> model = new HashMap<String, Object>();
        UserSession userSession = getUserSession(request);

        page = (page == null || page < 1) ? 1 : page;
        keyword = StringUtils.trimToNull(keyword);

        Pageable<Company> companyList = companyService.queryCompany(keyword, state, page);
        model.put("companyList", companyList);
        model.put("keyword", keyword);
        model.put("state", state);
        return new ModelAndView("company_list").addAllObjects(model);
    }
    /**
     * 获取公司列表
     * @param request
     * @param keyword 查询条件
     * @param state 状态（1：正常、2：停用）
     * @return HttpResponseBodyWrapper 公司列表数据
     * @throws Exception
     */
    @ResponseBody
    @GetMapping(value = "/company", produces = MediaType.APPLICATION_JSON_VALUE)
    public HttpResponseBodyWrapper getCompany(HttpServletRequest request, String keyword, Integer state, Integer page) throws Exception {
        Map<String, Object> model = new HashMap<String, Object>();
        UserSession userSession = getUserSession(request);
        List<Company> companyList = companyService.queryCompany(state);
        model.put("companyList", companyList);
        return new HttpResponseBodyWrapper(companyList);
    }
    /**
     * 新增公司
     * @param request
     * @param company 公司实体信息
     * @return HttpResponseBodyWrapper 封装操作标记（ true新增成功，false新增失败）
     * @throws Exception
     */
    @ResponseBody
    @PostMapping("/company")
    public HttpResponseBodyWrapper postCompany(HttpServletRequest request, @RequestBody Company company) throws Exception {
        Map<String, Object> model = new HashMap<String, Object>();
        UserSession userSession = getUserSession(request);

        company.setName(StringUtils.trimToNull(company.getName()));
        if (company.getName() == null) {
            throw new BizzException(BizzException.error_code_invalid_params, "请输入公司名称");
        }

        Boolean result = companyService.insertCompany(company);
        return new HttpResponseBodyWrapper(result);
    }
    /**
     * 根据公司id获取公司信息
     * @param request
     * @param id 公司id
     * @return HttpResponseBodyWrapper 包装单个公司实体数据
     * @throws Exception
     */
    @ResponseBody
    @GetMapping("/company/{id}")
    public HttpResponseBodyWrapper getCompany(HttpServletRequest request, @PathVariable Integer id) throws Exception {
        Map<String, Object> model = new HashMap<String, Object>();
        UserSession userSession = getUserSession(request);
        Company result = companyService.getCompany(id);
        return new HttpResponseBodyWrapper(result);
    }
    /**
     * 更新公司
     * @param request
     * @param company
     * @return HttpResponseBodyWrapper 封装操作标记（ true更新成功，false更新失败）
     * @throws Exception
     */
    @ResponseBody
    @PutMapping("/company/{id}")
    public HttpResponseBodyWrapper putCompany(HttpServletRequest request, @RequestBody Company company) throws Exception {
        Map<String, Object> model = new HashMap<String, Object>();
        UserSession userSession = getUserSession(request);

        company.setName(StringUtils.trimToNull(company.getName()));
        if (company.getName() == null) {
            throw new BizzException(BizzException.error_code_invalid_params, "请输入公司名称");
        }

        Boolean result = companyService.updateCompany(company);
        return new HttpResponseBodyWrapper(result);
    }
    /**
     * 删除公司
     * @param request
     * @param id 公司id
     * @return HttpResponseBodyWrapper 封装操作标记（ true删除成功，false删除失败）
     * @throws Exception
     */
    @ResponseBody
    @DeleteMapping("/company/{id}")
    public HttpResponseBodyWrapper deleteCompany(HttpServletRequest request, @PathVariable Integer id) throws Exception {
        Map<String, Object> model = new HashMap<String, Object>();
        UserSession userSession = getUserSession(request);
        Boolean result = companyService.deleteCompany(id);
        return new HttpResponseBodyWrapper(result);
    }
}
